package com.jhhc.StormSurgeForecast.StormSurge.ServiceImpl;

import com.jhhc.StormSurgeForecast.AstronomicalTide.Dao.BDMS_FBC_ELEMENTS_BMapper;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.FBC.BDMS_FBC_ELEMENTS_B;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.GZABO.BasicGrid;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.GZABO.Center;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.GZABO.Element;
import com.jhhc.StormSurgeForecast.StormSurge.Service.BDMS_FBC_ELEMENTS_BService;
import com.jhhc.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class BDMS_FBC_ELEMENTS_BServiceImpl implements BDMS_FBC_ELEMENTS_BService {

    @Autowired
    private BDMS_FBC_ELEMENTS_BMapper bdms_fbc_elements_bDao;

    @Autowired
    private RedisUtil redisTemplate;

    //获取基础网格数据
    @Override
    public Element GetCalDomainInfo(Integer ProjectId, String ElementNm) {
        Element element = (Element) redisTemplate.get("domain" + ProjectId + ElementNm);
        if (element == null) {
            List<BDMS_FBC_ELEMENTS_B> bdmsFbcElementsB = bdms_fbc_elements_bDao.GetCalDomainInfo(ProjectId,ElementNm);
            if (bdmsFbcElementsB != null && bdmsFbcElementsB.size() > 0 ) {
                element = new Element();
                for (BDMS_FBC_ELEMENTS_B elementsB : bdmsFbcElementsB) {
                    if (elementsB.getElementtp().equals(2)) {
                        String elementxy = elementsB.getElementxy().replace("|",";");
                        String[] xys = elementxy.split(";");
                        element.setGrids(xys.length);
                        List<BasicGrid> basicGrids = new ArrayList<>();
                        BasicGrid basicGrid = null;
                        for (String xy : xys) {
                            basicGrid = new BasicGrid();
                            String[] coordinate = xy.split(",");
//                            basicGrid.setX(Float.parseFloat(coordinate[0]));
//                            basicGrid.setY(Float.parseFloat(coordinate[1]));
                            basicGrid.setGridX(Float.parseFloat(coordinate[0]));
                            basicGrid.setGridY(Float.parseFloat(coordinate[1]));
                            basicGrids.add(basicGrid);
                        }
                        element.setBasicGrids(basicGrids);
                    }
                    if (elementsB.getElementtp().equals(6)) {
                        String elementxy = elementsB.getElementxy().replace("|",";");
                        String[] elements = elementxy.split(";");
                        element.setElements(elements.length);
                        List<String[]> e = new ArrayList<>();
                        for (String s : elements) {
                            e.add(s.split(","));
                        }
                        element.setElement(e);
                    }
                }
                element.setGridInfo(bdmsFbcElementsB.get(0).getElementnm() + "项目网格总数：" + element.getGrids() + "，面总数：" +
                        element.getElements());
            }
            redisTemplate.set("domain" + ProjectId + ElementNm,element);
            return element;
        }else {
            return element;
        }
    }

    @Override
    public Element GetCalDomainInfo(Integer projectId, String elementNm, String type) {
        Element element = (Element) redisTemplate.get("domain" + projectId + elementNm + type);
        if (element == null) {
            element = new Element();
            BDMS_FBC_ELEMENTS_B bdms_fbc_elements_b = bdms_fbc_elements_bDao.getCalDomainInfoDengLou(projectId,elementNm);
            if (bdms_fbc_elements_b != null) {
                List<String[]> list = new ArrayList<>();
                if (bdms_fbc_elements_b.getElementxy() != null && !"".equals(bdms_fbc_elements_b.getElementxy())) {
                    String[] xyList = bdms_fbc_elements_b.getElementxy().split("\\|");
                    element.setGrids(xyList.length);
                    int lenght = xyList[0].split(",").length;
                    String[] strings = null;
                    for (String xys : xyList) {
                        strings = new String[lenght];
                        String[] xy = xys.split(",");
                        for (int i = 0; i < xy.length; i++) {
                            strings[i] = xy[i];
                        }
                        list.add(strings);
                    }
                }
                element.setElementBases(list);
                redisTemplate.set("domain" + projectId + elementNm + type,element);
            }
            return element;
        }else {
            return element;
        }
    }

//    @Override
//    public Element GetCalDomainInfo(Integer ProjectId, String ElementNm, Integer grids) {
//        Element element = new Element();
//        List<BDMS_FBC_ELEMENTS_B> bdms_fbc_elements_b = (List<BDMS_FBC_ELEMENTS_B>) redisTemplate.get("Domain" + ProjectId);
//        if (bdms_fbc_elements_b == null) {
//            bdms_fbc_elements_b = bdms_fbc_elements_bDao.GetCalDomainInfo(ProjectId,ElementNm);
//            redisTemplate.set("Domain" + ProjectId,bdms_fbc_elements_b);
//            //redisTemplate.expire("bdms_fbc_elements_b" + ProjectId,36000);
//        }
//        String[] elemen = null;
//        String[] basicGrids = null;
//        int num = 0;
//        List<Integer> ElementOne = new ArrayList<>();
//        List<Integer> ElementTwo = new ArrayList<>();
//        List<Integer> ElementThree = new ArrayList<>();
//        List<Float> floatsX = new ArrayList<>();
//        List<Float> floatsy = new ArrayList<>();
//        for (BDMS_FBC_ELEMENTS_B b : bdms_fbc_elements_b) {
//            if (b.getElementtp().equals(2)) {
//                basicGrids = b.getElementxy().split(";");
//            }else if (b.getElementtp().equals(6)) {
//                elemen = b.getElementxy().split(";");
//                num = elemen[0].split(",").length;
//            }
//        }
//        assert basicGrids != null;
//        assert elemen != null;
//        if (bdms_fbc_elements_b.size() > 0) {
//            if (num == 3) {
//                for (int i = 0; i < grids; i++) {
//                    String[] basicGrid = basicGrids[i].split(",");
//                    String[] elemens = elemen[i].split(",");
//                    floatsX.add(Float.valueOf(basicGrid[0]));
//                    floatsy.add(Float.valueOf(basicGrid[1]));
//                    ElementOne.add(Integer.valueOf(elemens[0]));
//                    ElementTwo.add(Integer.valueOf(elemens[1]));
//                    ElementThree.add(Integer.valueOf(elemens[2]));
//                }
//                element.setBasicGridsX(floatsX);
//                element.setBasicGridsY(floatsy);
//                element.setElement1(ElementOne);
//                element.setElement2(ElementTwo);
//                element.setElement3(ElementThree);
//                element.setCount(num);
//            }else {
//                List<Integer> ElementFour = new ArrayList<>();
//                for (int i = 0; i < grids; i++) {
//                    String[] basicGrid = basicGrids[i].split(",");
//                    String[] elemens = elemen[i].split(",");
//                    floatsX.add(Float.valueOf(basicGrid[0]));
//                    floatsy.add(Float.valueOf(basicGrid[1]));
//                    ElementOne.add(Integer.valueOf(elemens[0]));
//                    ElementTwo.add(Integer.valueOf(elemens[1]));
//                    ElementThree.add(Integer.valueOf(elemens[2]));
//                    ElementFour.add(Integer.valueOf(elemens[3]));
//                }
//                element.setBasicGridsX(floatsX);
//                element.setBasicGridsY(floatsy);
//                element.setElement1(ElementOne);
//                element.setElement2(ElementTwo);
//                element.setElement3(ElementThree);
//                element.setElement4(ElementFour);
//                element.setCount(num);
//            }
//        }
//        return element;
//    }

    @Override
    public Center GetCalCentreInfo(Integer ProjectId, String ElementNm) {

        BDMS_FBC_ELEMENTS_B bdms_fbc_elements_b = (BDMS_FBC_ELEMENTS_B)redisTemplate.get("Centre" + ProjectId);
        if (bdms_fbc_elements_b == null) {
            bdms_fbc_elements_b = bdms_fbc_elements_bDao.GetCalCentreInfo(ProjectId, ElementNm);
            redisTemplate.set("Centre" + ProjectId,bdms_fbc_elements_b);
        }
        if (bdms_fbc_elements_b != null) {
            Center center = new Center();
            if (bdms_fbc_elements_b != null && !bdms_fbc_elements_b.getElementxy().equals("")
                    && bdms_fbc_elements_b.getElementxy()!=null) {
                List<Float> basicGridsX = new ArrayList<>();
                List<Float> basicGridsY = new ArrayList<>();
                String[] basicGrids = bdms_fbc_elements_b.getElementxy().split(";");
                for (String grid : basicGrids) {
                    String[] basicGrid = grid.split(",");
                    basicGridsX.add(Float.parseFloat(basicGrid[0]));
                    basicGridsY.add(Float.parseFloat(basicGrid[1]));
                }
                center.setBasicGridsX(basicGridsX);
                center.setBasicGridsY(basicGridsY);
            }
            return center;
        }else {
            return null;
        }
    }

//    @Override
//    public Center GetCalCentreInfo(Integer ProjectId, String ElementNm, Integer grids) {
//        BDMS_FBC_ELEMENTS_B bdms_fbc_elements_b = (BDMS_FBC_ELEMENTS_B)redisTemplate.get("Centre" + ProjectId);
//        if (bdms_fbc_elements_b == null) {
//            bdms_fbc_elements_b = bdms_fbc_elements_bDao.GetCalCentreInfo(ProjectId, ElementNm);
//            redisTemplate.set("Centre" + ProjectId,bdms_fbc_elements_b);
//        }
//        Center center = new Center();
//        if (bdms_fbc_elements_b != null && !bdms_fbc_elements_b.getElementxy().equals("") && bdms_fbc_elements_b.getElementxy()!=null) {
//            List<Float> basicGridsX = new ArrayList<>();
//            List<Float> basicGridsY = new ArrayList<>();
//            String[] basicGrids = bdms_fbc_elements_b.getElementxy().split(";");
//            if (grids != null) {
//                for (int i = 0; i < grids; i++) {
//                    String[] basicGrid = basicGrids[i].split(",");
//                    basicGridsX.add(Float.parseFloat(basicGrid[0]));
//                    basicGridsY.add(Float.parseFloat(basicGrid[1]));
//                }
//            }
//            center.setBasicGridsX(basicGridsX);
//            center.setBasicGridsY(basicGridsY);
//        }
//        return center;
//    }
}
